﻿using COSXML;
using COSXML.Auth;
using COSXML.CosException;
using COSXML.Model.Object;
using Md.Api.DDD;
using Md.Api.Model.Util.Oss.Request;
using Md.Api.Model.Util.Oss.Response;

namespace Md.Util.Service.Oss;
/// <summary>
/// 腾讯云Cos文件上传服务
/// </summary>
public class TencentCosUploadService : ApiBase<RequestTencentCosUploadModel>
{
    private CosXml cosXml;
    /// <summary>
    /// 执行
    /// </summary>
    protected override void ExecuteMethod()
    {
        try
        {
            if (string.IsNullOrWhiteSpace(Parameter.LocalFilePath))
            {
                throw new AggregateException("文件本地路径不能为空");
            }

            if (string.IsNullOrWhiteSpace(Parameter.YunFilePath))
            {
                throw new AggregateException("云文件路径不能为空");
            }

            //初始化 CosXmlConfig
            var bucketname = JsonConfig.JsonRead("MdTencent:Cos:BucketName");
            var appid = JsonConfig.JsonRead("MdTencent:Cos:APPID"); // 设置腾讯云账户的账户标识 APPID
            var region = JsonConfig.JsonRead("MdTencent:Cos:Region"); //设置一个默认的存储桶地域
            var secretId = JsonConfig.JsonRead("MdTencent:SecretId");
            var secretKey = JsonConfig.JsonRead("MdTencent:SecretKey");

            CosXmlConfig config = new CosXmlConfig.Builder()
              .IsHttps(true)  //设置默认 HTTPS 请求
              .SetRegion(region)  //设置一个默认的存储桶地域
              .SetDebugLog(true)  //显示日志
              .Build();  //创建 CosXmlConfig 对象

            long durationSecond = 600; //每次请求签名有效时长，单位为秒

            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);

            cosXml = new CosXmlServer(config, qCloudCredentialProvider);

            // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
            string bucket = bucketname;
            string key = Parameter.YunFilePath; //对象键
            string srcPath = Parameter.LocalFilePath;//本地文件绝对路径


            PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);

            //设置进度回调
            //request.SetCosProgressCallback(delegate (long completed, long total)
            //{
            //    Console.WriteLine(string.Format("progress = {0:##.##}%", completed * 100.0 / total));
            //});

            //执行请求
            PutObjectResult result = cosXml.PutObject(request);
            //对象的 eTag
            // string eTag = result.eTag;

            var res = new ResponseTencentCosUploadModel();
            res.LocalFilePath = Parameter.LocalFilePath;
            res.YunFilePath = $"{JsonConfig.JsonRead("MdTencent:Cos:Domain")}/{Parameter.YunFilePath}";

            Result.Data = res;
            Result.Msg = "success";
        }
        catch (CosClientException clientEx)
        {
            throw new AggregateException(clientEx.Message);
        }
        catch (CosServerException serverEx)
        {
            throw new AggregateException(serverEx.Message);
        }
    }
}

